package com.logAndSignProvider.service.impl;

import com.logAndSignProvider.mapper.UserMapper;
import com.logAndSignProvider.service.UserService;
import entity.ResponseMessage;
import entity.StatusCode;
import entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.JWTUtil;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * The type User service.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    private JWTUtil jwtUtil;
    private Logger logger = Logger.getLogger(this.getClass());

    /**
     * @Author: overlook
     * @Created by 2022年04月13日
     * @param: * @param user
     * @return: entity.ResponseMessage
     * @Description: 登录查询所有用户 登录
     */
    @Override
    public ResponseMessage findAllUser(User user) {
        ResponseMessage msg = new ResponseMessage();
        //取出前端传来的用户名及密码
        String password = user.getPassword();
        String userName = user.getUserName();
        logger.info("UserServiceImpl findAllUser params:" + user);
        try {
            //将前端传的用户名封装到新的user对象中
            User selectByUserName = new User();
            selectByUserName.setUserName(userName);
            //通过前端传来的userName查询用户
            List<User> allUserByUserName = userMapper.findAllUser(selectByUserName);
            logger.debug("UserServiceImpl findAllUser selectByUserName params:" + selectByUserName + ",result:" + allUserByUserName);
            //因为用户名唯一，通过userName查询的只能是一个
            User queryByUserName = allUserByUserName.get(0);
            //如果查询结果不为空，则将查询结果的密码和前端传来的密码匹配，成功则密码正确
            if (queryByUserName != null && encoder.matches(password, queryByUserName.getPassword())) {
                msg.setCode(StatusCode.OK);
                msg.setData(queryByUserName);
                msg.setFlag(true);
                msg.setMessage("ok");
                //将用户信息存入redis，过期时间为30分钟
                ValueOperations<String, User> operations = redisTemplate.opsForValue();
                operations.set(queryByUserName.getUserName(), queryByUserName, 30, TimeUnit.MINUTES);
                //生成token信息
                jwtUtil.setTtl(3600000);
                jwtUtil.setKey("secrect");
                String token = jwtUtil.createJWT(queryByUserName.getId(), queryByUserName.getUserName(), queryByUserName.getRoles());
                msg.setToken(token);
                logger.debug("UserServiceImpl findAllUser params:" + user + ",result:" + queryByUserName);
                return msg;
            }
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("find all user result is empty");
            logger.debug("UserServiceImpl findAllUser params:" + user + ",result:" + queryByUserName);
            return msg;
        } catch (Exception e) {
            //异常情况
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage(e.getMessage());
            msg.setFlag(false);
            msg.setData(e);
            logger.debug("UserServiceImpl findAllUser params:" + user);
            return msg;
        }
    }
}
